package cn.Liujialin.user.controller;

import cn.Liujialin.basic.util.AjaxResult;
import cn.Liujialin.cation.BusinessException;
import cn.Liujialin.user.dto.LoginDto;
import cn.Liujialin.user.service.ILoginDtoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILoginDtoService iLoginDtoService;

    /*
    前后端共用接口
    */
   //要返回一个字符串token和 loginInfo基本信息 少量可以用map封装
    @PostMapping("/account")
    public AjaxResult Login(@RequestBody LoginDto loginDto){
        try {
            Map<String,Object> map = iLoginDtoService.accountLogin(loginDto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            //fail返回Str
            e.printStackTrace();
            return AjaxResult.fail(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("登录失败，后端出问题");
        }
    }

    /**
     微信扫码登录
     */
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody Map<String,String> params){
        try {
            //可能：返回 token串和 loginInfo基本信息 或者 accessToken + openId
            return iLoginDtoService.wechat(params);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("登录失败，后端出问题！");
        }

    }
    /**
     微信绑定
     */
    @PostMapping("/binderwechat")
    public AjaxResult binderwechat(@RequestBody Map<String,String> params){
        try {
            //可能：返回 token串和 loginInfo基本信息 或者 accessToken + openId
            return iLoginDtoService.binderwechat(params);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("登录失败，后端出问题！");
        }

    }



}
